<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
            <el-form-item label="仓库名称" prop="warehouseName">
                <el-input v-model="queryParams.warehouseName" placeholder="请输入仓库名称" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="仓库编码" v-if="false" prop="warehouseCode">
                <el-input v-model="queryParams.warehouseCode" placeholder="请输入仓库编码" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="仓库管理员id" prop="warehouseAdminid">
        <el-input
          v-model="queryParams.warehouseAdminid"
          placeholder="请输入仓库管理员id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
            <el-form-item label="仓库管理员电话" prop="warehousePhone" label-width="120px">
                <el-input v-model="queryParams.warehousePhone" placeholder="请输入仓库管理员电话" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="仓库简称" v-if="false" prop="warehouseForshort">
                <el-input v-model="queryParams.warehouseForshort" placeholder="请输入仓库简称" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="仓库别名" v-if="false" prop="warehouseAlise">
                <el-input v-model="queryParams.warehouseAlise" placeholder="请输入仓库别名" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="仓库简介" v-if="false" prop="warehouseIntro">
                <el-input v-model="queryParams.warehouseIntro" placeholder="请输入仓库简介" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="仓库库容" v-if="false" prop="warehouseNum">
                <el-input v-model="queryParams.warehouseNum" placeholder="请输入仓库库容" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="仓库类型" prop="warehouseType">
                <el-select v-model="queryParams.warehouseType" placeholder="请选择仓库类型" clearable size="small"
                    @change="handleQuery">
                    <el-option label="固态" value="1" />
                    <el-option label="液态" value="2" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:warehouse:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:warehouse:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['system:warehouse:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:warehouse:export']">导出</el-button>
            </el-col>
        </el-row>

        <el-table v-loading="loading" :data="warehouseList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!-- <el-table-column
        label="主键id"
        align="center"
        prop="id"
      /> -->
            <el-table-column label="仓库名称" align="center" prop="warehouseName" />
            <el-table-column label="仓库编码" align="center" prop="warehouseCode" />
            <el-table-column label="仓库管理员id" v-if="false" align="center" prop="warehouseAdminid" />
            <el-table-column label="仓库管理员电话" align="center" prop="warehousePhone" />
            <el-table-column label="仓库简称" align="center" prop="warehouseForshort" />
            <el-table-column label="仓库别名" align="center" prop="warehouseAlise" />
            <el-table-column label="仓库简介" align="center" prop="warehouseIntro" />
            <el-table-column label="仓库库容" align="center" prop="warehouseNum" />
            <el-table-column label="仓库类型" align="center" prop="warehouseType">
                <template slot-scope="scope">
                    <span v-if="scope.row.warehouseType == 1">固态</span>
                    <span v-if="scope.row.warehouseType == 2">液态</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:warehouse:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['system:warehouse:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改仓库对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="仓库名称" prop="warehouseName">
                    <el-input v-model="form.warehouseName" placeholder="请输入仓库名称" />
                </el-form-item>
                <el-form-item label="仓库编码" prop="warehouseCode">
                    <el-input v-model="form.warehouseCode" placeholder="请输入仓库编码" />
                </el-form-item>
                <el-form-item v-if="false" label="仓库管理员id" prop="warehouseAdminid">
                    <el-input v-model="form.warehouseAdminid" placeholder="请输入仓库管理员id" />
                </el-form-item>
                <el-form-item label="仓库管理员电话" prop="warehousePhone">
                    <el-input v-model="form.warehousePhone" placeholder="请输入仓库管理员电话" @change="getAdminPhone" />
                </el-form-item>
                <el-form-item label="仓库简称" prop="warehouseForshort">
                    <el-input v-model="form.warehouseForshort" placeholder="请输入仓库简称" />
                </el-form-item>
                <el-form-item label="仓库别名" prop="warehouseAlise">
                    <el-input v-model="form.warehouseAlise" placeholder="请输入仓库别名" />
                </el-form-item>
                <el-form-item label="仓库简介" prop="warehouseIntro">
                    <el-input v-model="form.warehouseIntro" placeholder="请输入仓库简介" />
                </el-form-item>
                <el-form-item label="仓库库容" prop="warehouseNum">
                    <el-input v-model="form.warehouseNum" placeholder="请输入仓库库容" />
                </el-form-item>
                <el-form-item label="仓库类型" prop="warehouseType">
                    <el-select v-model="form.warehouseType" placeholder="请选择仓库类型" clearable size="small"
                        style="width: 100%;">
                        <el-option v-for="item in warehouses" :key="item.id" :label="item.id"
                            :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listWarehouse,
    getWarehouse,
    delWarehouse,
    addWarehouse,
    updateWarehouse,
    exportWarehouse,
} from "@/api/warehouse/warehouse";
import TestInfot from "../productTestInfo/testInfot/index";

export default {
    name: "Warehouse",
    components: { TestInfot },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            //仓库类型
            warehouses: [
                { id: "固态", value: '1' },
                { id: "液态", value: '2' },
            ],
            // 总条数
            total: 0,
            // 仓库表格数据
            warehouseList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                warehouseName: undefined,
                warehouseCode: undefined,
                warehouseAdminid: undefined,
                warehousePhone: undefined,
                warehouseForshort: undefined,
                warehouseAlise: undefined,
                warehouseIntro: undefined,
                warehouseNum: undefined,
                warehouseType: undefined,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                warehouseName: [
                    { required: true, message: "请输入仓库名称", trigger: "change" },
                ], warehouseCode: [
                    { required: true, message: "请输入仓库编码", trigger: "change" },
                ], warehousePhone: [
                    { required: true, message: "请输入仓库管理员电话", trigger: "change" },
                ], warehouseNum: [
                    { required: true, message: "请输入仓库库容", trigger: "change" },
                ], warehouseType: [
                    { required: true, message: "请输入仓库类型", trigger: "change" },
                ],
            },
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询仓库列表 */
        getList() {
            this.loading = true;
            listWarehouse(this.queryParams).then((response) => {
                this.warehouseList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        /* phone 获取  管理员 id  */
        getAdminPhone(val) {
            console.log(`管理员id：`, val)
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                warehouseName: undefined,
                warehouseCode: undefined,
                warehouseAdminid: undefined,
                warehousePhone: undefined,
                warehouseForshort: undefined,
                warehouseAlise: undefined,
                warehouseIntro: undefined,
                warehouseNum: undefined,
                warehouseType: undefined,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length != 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加仓库";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getWarehouse(id).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = "修改仓库";
            });
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.id != undefined) {
                        updateWarehouse(this.form).then((response) => {
                            this.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addWarehouse(this.form).then((response) => {
                            this.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm('是否确认删除仓库编号为"' + ids + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(function () {
                    return delWarehouse(ids);
                })
                .then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                })
                .catch(function () { });
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$confirm("是否确认导出所有仓库数据项?", "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(function () {
                    return exportWarehouse(queryParams);
                })
                .then((response) => {
                    this.download(response.data);
                })
                .catch(function () { });
        },
    },
};
</script>
